﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
using Zero.Common;

namespace Example.Entities
{
    [DataContract(IsReference = true)]
    [KnownType(typeof(Order))]
    public class Customer : EntityObject
    {
        #region Primitive Properties

        [DataMember, Key, Required]
        public int Id
        {
            get { return _id; }
            set
            {
                if (_id != value)
                {
                    _id = value;
                    OnPropertyChanged("Id");
                }
            }
        }

        private int _id;

        [DataMember, StringLength(50), Required]
        public string Name
        {
            get { return _name; }
            set
            {
                if (_name != value)
                {
                    _name = value;
                    OnPropertyChanged("Name");
                }
            }
        }
        private string _name;

        [DataMember, Required, StringLength(50)]
        public string Surname
        {
            get { return _surname; }
            set
            {
                if (_surname != value)
                {
                    _surname = value;
                    OnPropertyChanged("Surname");
                }
            }
        }
        private string _surname;

        [DataMember, Required]
        public DateTime BirthDate
        {
            get { return _birthDate; }
            set
            {
                if (_birthDate != value)
                {
                    _birthDate = value;
                    OnPropertyChanged("BirthDate");
                }
            }
        }

        private DateTime _birthDate;

        #endregion

        #region Navigation Properties

        [DataMember]
        public ObservableCollection<Order> Orders
        {
            get
            {
                return _orders;
            }
            set
            {
                if (!ReferenceEquals(_orders, value))
                {
                    _orders = value;

                    OnPropertyChanged("Orders");
                }
            }
        }
        private ObservableCollection<Order> _orders;

        #endregion        

        public override int GetHashCode()
        {
            return Id.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            return obj is Customer && (obj as Customer).Id == Id;
        }
    }
}
